import 'package:baby/Entities/Comment/Comment.dart';
import 'package:baby/Entities/Record/PhotoRecord.dart';
import 'package:baby/Entities/Record/RecordTag.dart';
import 'package:baby/Entities/Record/RecordType.dart';
import 'package:baby/Entities/Record/TextRecord.dart';
import 'package:baby/Entities/Record/VideoRecord.dart';
import 'package:baby/UI/Widgets/ListDividerGenerator.dart';
import 'package:flutter/material.dart';

import 'RecordList.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin<HomePage>, AutomaticKeepAliveClientMixin {
  final maxExpandPercent = 60.0;

  var textStyle = TextStyle(color: Colors.white);
  var position = 0.0;
  var items = <TextRecord>[];
  AnimationController scrollAnimationController;
  Animation<double> scrollAnimation;

  var babies = List.generate(6, (index) => index);

  @override
  void initState() {
    scrollAnimationController = AnimationController(vsync: this, duration: Duration(milliseconds: 250));

    void addItems() {
      items.add(PhotoRecord(
        tag: RecordTag(
          icon: "https://banner2.kisspng.com/20180509/zde/kisspng-avatar-youtube-8-ball-pool-user-5af328dc1be026.0480517715258851481142.jpg",
          label: "دۇنياغا كۆز ئاچتىڭ"
        ),
        content: "ئۇ تۇغۇلدى",
        type: RecordType.Photo,
        photos: [
          "https://www.bloomingbath.com/media/wysiwyg/Downloads/IMG_1690.jpg",
          "https://images-na.ssl-images-amazon.com/images/G/01/aplusautomation/vendorimages/94ad8a08-62e4-46f1-84a4-a7c1481c6564.jpg._CB294152044_.jpg",
          "https://beachfrontbabywraps.com/wp-content/uploads/2016/03/pbsling-Cropped.jpg",
          "https://sep.yimg.com/ay/albee-baby/baby-k-tan-baby-carrier-in-natural-organic-extra-large-40.jpg"
        ],
      ));

      var comment0 = Comment(
        content: "ۋاي، بەك يىغلايدىكەنلا.😼",
        dateTime: DateTime.now(),
        likeCount: 10,
        unlikeCount: 3,
      );
      var comment1 = Comment(
        content: "ھە شۇندا",
        dateTime: DateTime.now(),
        likeCount: 10,
        unlikeCount: 3,
        replayTo: comment0
      );
      var comment2 = Comment(
        content: "شۇندامىتە.😼",
        dateTime: DateTime.now(),
        likeCount: 10,
        unlikeCount: 3,
        replayTo: comment1
      );
      var comment3 = Comment(
        content: "بۇ سۆزلەپ كەلسەك گەپ جىق ، بۇ بۇندىن ئىككى ئاي بۇرۇن بولغان ۋەقە . ئەڭ ئاۋۋال توربەتنىڭ ئۈندىدار كىچىك پىروگراممىسىدىن چاتاق چىقتى . يەنى api نىڭ https دىن چاتاق چىقتى. ۋاقتى ئۆتەپ كىتىپتىمىشلەر كىيىن ئۇقسام .شۇنىڭ بىلەن بىرە كۈن ئىيتىشىپ مۇلازىمىتىېرنى ئاندا مۇندا قىلىپ ئىشقىلىپ مالماتاڭ قىلىپ ئېغىلدەكلا قىپ قويدۇم",
        dateTime: DateTime.now(),
        likeCount: 0,
        unlikeCount: 10,
      );
      var comment4 = Comment(
        content: "كاللىسىدىن كەتكەن بالىكەن ما ئۈستىدىكىسى",
        dateTime: DateTime.now(),
        likeCount: 10,
        unlikeCount: 3,
        replayTo: comment3
      );

      items.add(PhotoRecord(
        content: "يىغلا بالام، تازا يىغلا، تەڭ يىغلايلى...😭 ئۇھ...",
        type: RecordType.Photo,
        photos: [
          "https://www.boldsky.com/img/2014/07/15-mancrying.jpg",
          "http://illumination.missouri.edu/s13/images/nn/cry_baby.jpg",
          "https://assets.parents.com/s3fs-public/styles/nfp_1080_portrait/public/ZdorovKirillVladimirovich_0.jpg?HZo9_mI3Bdq5KNVEKWPF9SPETsqtRISw",
          "http://www.evolutionaryparenting.com/wp-content/uploads/2011/08/crying-baby.jpg",
          "http://www.slate.com/content/dam/slate/blogs/behold/2013/08/Jill%20Greenberg/8.jpg.CROP.original-original.jpg"
        ],
        comments: [
          comment0,
          comment1,
          comment2,
          comment3,
          comment4
        ]
      ));

      items.add(TextRecord(
          type: RecordType.Text,
          content: "شۇنىڭ بىلەن ساڭا بىر ئىسىم قويىلى دەپ بالام، مەن قويىمەن دىگەننى ئاپاڭ دىگەن خوتۇن قويغىلى قويمىدى..."
      ));

      items.add(VideoRecord(
          content: "ئاپاڭ دىگەن ساراڭ خوتۇن بىلەن ئۇرۇشۇپ قالدۇق بالام.",
          type: RecordType.Photo,
          videos: [
            "https://carwad.net/sites/default/files/pictures-of-husband-and-wife-fighting-150433-1976937.jpg",
            "https://www.premierboxingchampions.com/sites/default/files/Vargas-vs-Herrera.jpg",
            "http://media.salemwebnetwork.com/cms/CW/Couples/33985-fight-couplefighting-marriage.1200w.tn.jpg",
            "https://cn.bing.com/th?id=OIP.ottiQm0wCZLdDEAGGcDM-AAAAA&pid=Api&rs=1&p=0",
          ]
      ));

      items.add(TextRecord(
          type: RecordType.Text,
          content: "كومپىيۇتېر ئىلمىنىڭ ئىككىنچى چوڭ تارمىقى كومپىيۇتېر قۇرۇلۇشى. كومپىيۇتېرنى لايىھەلەش تولىمۇ چوڭ جەڭنىڭ بىرى، چۈنكى ئاستىنقى قاتلامدىكى قاتتىق دېتالدىن ئۈستىنكى قاتلامدىكى يۇمشاق دېتالغىچە نۇرغۇن ئوخشىمايدىغان تەرەپلەرنى ئويلىشىشقا توغرا كېلىدۇ. لايىھەلىگۈچى چوقۇم كومپىيۇتېرنى ئىمكانقەدەر ئەلالاشتۇرۇشتۇرۇپ قۇربىنىڭ يېتىشىچە تېخىمۇ كۆپ مەسىلەرنى ھەل قىلىشقا كاپالەتلىك قىلىشى كېرەك"
      ));
    }
    addItems();
    addItems();
    addItems();

    super.initState();
  }

  @override
  void didUpdateWidget(HomePage oldWidget) {


    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    scrollAnimationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (scrollAnimation == null) {
      var curve = CurvedAnimation(parent: scrollAnimationController, curve: Curves.easeInOutCubic);
      scrollAnimation = Tween<double>(
          begin: 0.0,
          end: (MediaQuery.of(context).size.height / 100) * maxExpandPercent
      ).animate(curve);
    }

    return Stack(
      children: <Widget>[
        SizedBox(
          height: (MediaQuery.of(context).size.height / 100) * maxExpandPercent,
          child: Column(
            children: <Widget>[
              Expanded(
                child: ListView(
                  children: ListDividerGenerator(babies.map((index) => ListTile(
                    title: Text("بوۋىقام: $index"),
                    leading: CircleAvatar(
                      radius: 28.0,
                      backgroundColor: Theme.of(context).primaryColor,
                      backgroundImage: ExactAssetImage("assets/images/baby.jpg"),
                    ),
                    trailing: Wrap(
                      children: <Widget>[
                        IconButton(icon: Icon(Icons.edit), onPressed: (){}),
                        IconButton(icon: Icon(Icons.delete), onPressed: (){}),
                      ],
                    ),
                    onTap: (){
                      scrollAnimationController.reverse();
                    },
                  )).toList(), Divider()).items,
                ),
              )
            ],
          ),
        ),

        AnimatedBuilder(
          animation: scrollAnimation,
          builder: (context, child) => Transform.translate(
            offset: Offset(0.0, scrollAnimation.value),
            child: child,
          ),
          child: Container(
            color: Colors.white,
            child: CustomScrollView(
              slivers: <Widget>[
                SliverAppBar(
                  automaticallyImplyLeading: false,
                  expandedHeight: 230.0,
                  flexibleSpace: FlexibleSpaceBar(
                    background: Stack(
                      children: <Widget>[
                        SizedBox(
                          child: Image.asset("assets/images/family.jpg", fit: BoxFit.cover,),
                          width: double.infinity,
                          height: double.infinity,
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 30.0),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Padding(
                                  padding: EdgeInsets.all(10.0),
                                  child: CircleAvatar(
                                    radius: 45.0,
                                    backgroundColor: Theme.of(context).primaryColor,
                                    backgroundImage: ExactAssetImage("assets/images/baby.jpg"),
                                  )
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Text("ئىسمى", style: textStyle),
                                  Text("ئائىلە 2", style: textStyle),
                                ],
                              ),
                            ],
                          ),
                        ),
                        Align(
                          alignment: Alignment(0.0, 1.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: <Widget>[
                              FlatButton(
                                child: Text("ئالبۇملار", style: textStyle,),
                                onPressed: (){},
                              ),
                              FlatButton(
                                child: Text("خاتىرە", style: textStyle,),
                                onPressed: (){},
                              ),
                              FlatButton(
                                child: Text("چوڭ ئىشلار", style: textStyle,),
                                onPressed: (){},
                              ),
                              FlatButton(
                                child: Text("قىرىندىشىم: 2", style: textStyle,),
                                onPressed: (){},
                              ),
                            ],
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 25.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              FloatingActionButton(
                                child: Icon(Icons.menu),
                                onPressed: (){
                                  var closed = scrollAnimationController.status == AnimationStatus.forward || scrollAnimationController.status == AnimationStatus.completed;
                                  closed ? scrollAnimationController.reverse() : scrollAnimationController.forward();
                                },
                                mini: true,
                                heroTag: "HomePage-MenuButton",
                              ),
                              FloatingActionButton(
                                child: Icon(Icons.camera_enhance),
                                onPressed: (){

                                },
                                mini: true,
                                heroTag: "HomePage-CameraButton",
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                SliverList(
                  delegate: SliverChildListDelegate([
                    Padding(
                        padding: EdgeInsets.symmetric(horizontal: 2.0),
                        child: RecordList(records: items)
                    )
                  ]),
                )
              ],
            ),
          ),
        )
      ],
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

